package com.yxjx.duoxue.my;

import java.io.IOException;

import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.alipay.sdk.data.Response;
import com.yxjx.duoxue.BaseActionBarActivity;
import com.yxjx.duoxue.R;
import com.yxjx.duoxue.customview.autolist.TestActivity;
import com.yxjx.duoxue.datamodel.ResponseObject;
import com.yxjx.duoxue.mta.stat.StatHelper;
import com.yxjx.duoxue.network.MsgCode;
import com.yxjx.duoxue.network.NwUtils;
import com.yxjx.duoxue.network.UserReg;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

public class FeedbackActivity extends BaseActionBarActivity {
	
	private Context mContext;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.fragment_feedback);
		customizeActionBarTitle("意见反馈");
		UIHelper.setClickListener(findViewById(R.id.root), R.id.submit, mClickListener);
		mInput = (EditText) findViewById(R.id.input);
	}
	
	private View.OnClickListener mClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(final View arg0) {
			final String inputText = mInput.getText().toString();
			switch (arg0.getId()) {
			case R.id.submit:
				if (Utils.getNetworkInfo(FeedbackActivity.this) == Utils.NW_TYPE_NO_CONN) {
					Toast.makeText(FeedbackActivity.this, "无网络链接！", Toast.LENGTH_SHORT).show();
				} else if (inputText.length() <= 0 || inputText.length() > 200) {
					Toast.makeText(FeedbackActivity.this, "输入文字长度必须大于0并且不超过200", Toast.LENGTH_SHORT).show();
				} else {
					new Thread(new Runnable() {
						@Override
						public void run() {
							ResponseObject code = new ResponseObject(-1, "");
							try {
								code = NwUtils.submitFeedback(getApplicationContext(), inputText);
							} catch (ClientProtocolException e) {
								e.printStackTrace();
							} catch (ParseException e) {
								e.printStackTrace();
							} catch (IOException e) {
								e.printStackTrace();
							} catch (JSONException e) {
								e.printStackTrace();
							}
							
							final ResponseObject finalCode = code;
							arg0.postDelayed(new Runnable() {
								@Override
								public void run() {
									Toast.makeText(FeedbackActivity.this, 
											finalCode.getCode() == MsgCode.SUCCESS ? "提交成功":"提交失败，"  + MsgCode.descFor(finalCode), 
											Toast.LENGTH_SHORT).show();
									StatHelper.addKVEvent(mContext, "event_my_feedback_submit");
									FeedbackActivity.this.finish();
								}
							}, 0);
						}
					}).start();
					
				}
				break;
			default:
				break;
			}
			
		}
	};
	


	private EditText mInput;}
